{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# API 文档介绍（Azure API）\n",
    "* 本周主要内容：API文档阅读介绍及计算机视觉入门（认知服务）\n",
    "* 20春_API_人工智能与机器学习_week02\n",
    "*  电子讲义设计者：许智超，廖汉腾\n",
    "\n",
    "## 本周内容及学习目标\n",
    "\n",
    "本周内容聚集在复习1中的计算机视觉，以及复习2中的API操作部分，学习解决一下挑战：\n",
    "\n",
    "1. 尝试操作计算机视觉人脸识别返回[人脸识别效果](https://azure.microsoft.com/zh-cn/services/cognitive-services/face/)\n",
    "2. 阅读Azure计算机视觉的[人脸文档](https://docs.microsoft.com/zh-cn/azure/cognitive-services/face/)，以及[人脸 API v1.0文档](https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236)\n",
    "2. 观看影片[知智1分钟计算机视觉](https://zhuanlan.zhihu.com/p/35652529) 与[知智1分钟人脸识别](https://zhuanlan.zhihu.com/p/36262110)\n",
    "3. 注册Azure 使用API免费服务，获取key以为获取API应用做准备\n",
    "4. 使用requests，用代码取得API回复\n",
    "5. 写出代码，实现输入一个图片URL，可以识别出每个人脸的年龄、性别、眼镜的，加总成绩1分；列出每个人最可能的3种情绪及其判别值再加1分。\n",
    "6. 使用pandas 将返回数据用数据框展示出来。\n",
    "\n",
    "## 作业\n",
    "1. 设计一款你觉得需要人脸功能的产品。\n",
    "2. 尝试使用三个API开放平台去实现（希望一定有Azure）。\n",
    "3. 做出来数据结构化的表格，去阐述你的功能附加加值。\n",
    "4. 放到github上边"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Microsoft Azure API(面部检测) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第1个图片尝试，多人图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 1、导入需要的requests模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 先导入为们需要的模块\n",
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 2、输入我们需要API网站注册的API_Key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "KEY = 'e969c474c0df4357a1f2a9dbf3b2329a'  # Replace with a valid Subscription Key here.在这里使用有效的订阅密钥替换。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 3、目标url [base url] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Base URL,  Request URL中 符号?以前\n",
    "BASE_URL = 'https://zoe-apiface.cognitiveservices.azure.com/face/v1.0/detect' \n",
    "\n",
    "# 输入自己的KEY值\n",
    "KEY = 'e969c474c0df4357a1f2a9dbf3b2329a'  # 在这里使用有效的订阅密钥替换。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 4、沿用API文档的示范代码,准备我们的headers和图片(数据)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 沿用API的示范代碼，{subscription key}用KEY代入\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY),\n",
    "}\n",
    "\n",
    "img_url = 'http://picm.bbzhi.com/mingxingbizhi/feilunhaifeilunhaibizhi/star_starhk_141010_m.jpg' # 一张男团，飞轮海，*年轻*时候的照片哈哈哈"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 5、准备symbol ? 后面的数据,这里需要注意,一定要详细阅读API文档中的 “参数功能”,按照要求格式准备payload\n",
    "\n",
    "* 参数功能可能有:\n",
    "    * 1、是否必要?必要的一定要准备好\n",
    "    * 2、选填的一定是功能,要根据功能需求 好好填噢"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses,emotion'),# 年龄，性别，眼睛，情感\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 6、requests发送我们请求\n",
    "\n",
    "* 注意:\n",
    "    * 详细阅读文档,注意请求方式(GET、POST、DELETE)\n",
    "    * 注意json 和字典的差异 ,str vs dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'a0934bcf-e4fd-435e-b9d7-8f1e4b8c0dc3',\n",
       "  'faceRectangle': {'top': 165, 'left': 143, 'width': 65, 'height': 65},\n",
       "  'faceAttributes': {'gender': 'male',\n",
       "   'age': 22.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.001,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.81,\n",
       "    'neutral': 0.187,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.002}}},\n",
       " {'faceId': 'd5099372-010d-4d2d-9a65-143775e18684',\n",
       "  'faceRectangle': {'top': 172, 'left': 289, 'width': 58, 'height': 58},\n",
       "  'faceAttributes': {'gender': 'male',\n",
       "   'age': 22.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.985,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.014}}},\n",
       " {'faceId': '3331f6cc-b137-48e0-b174-2987a5d357ee',\n",
       "  'faceRectangle': {'top': 100, 'left': 425, 'width': 58, 'height': 58},\n",
       "  'faceAttributes': {'gender': 'male',\n",
       "   'age': 21.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.98,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.019}}},\n",
       " {'faceId': 'b19d1a74-ec12-4536-b67f-a3dab1e8e4b2',\n",
       "  'faceRectangle': {'top': 85, 'left': 569, 'width': 56, 'height': 56},\n",
       "  'faceAttributes': {'gender': 'male',\n",
       "   'age': 20.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 1.0,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0}}}]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 坑。参考http://docs.python-requests.org/zh_CN/latest/user/quickstart.html  【更加复杂的post请求】\n",
    "# 差別是 string 字串 vs. dict 字典\n",
    "# Azura 使用的是 data = json.dumps(payload) 或 json=payload，data = payload 会出错\n",
    "\n",
    "# json.dumps()，将字典格式转换成json格式\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)  # HTTP? POST? 请求参数？\n",
    "\n",
    "r.status_code  # 查看参数回传状态\n",
    "results = r.json()  # 将回传数据转化成json格式\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 拿到数据会不会很开心? 不过我们还可以做的更好\n",
    "* 别忘记我们是会基本处理数据的,至少要想到用pandas表格化数据."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>眼镜</th>\n",
       "      <th>生气</th>\n",
       "      <th>蔑视</th>\n",
       "      <th>厌恶</th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>faceId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a0934bcf-e4fd-435e-b9d7-8f1e4b8c0dc3</th>\n",
       "      <td>男性</td>\n",
       "      <td>22.0</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.810</td>\n",
       "      <td>0.187</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d5099372-010d-4d2d-9a65-143775e18684</th>\n",
       "      <td>男性</td>\n",
       "      <td>22.0</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.985</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3331f6cc-b137-48e0-b174-2987a5d357ee</th>\n",
       "      <td>男性</td>\n",
       "      <td>21.0</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.980</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b19d1a74-ec12-4536-b67f-a3dab1e8e4b2</th>\n",
       "      <td>男性</td>\n",
       "      <td>20.0</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      性别    年龄    眼镜   生气     蔑视   厌恶   恐惧  \\\n",
       "faceId                                                                       \n",
       "a0934bcf-e4fd-435e-b9d7-8f1e4b8c0dc3  男性  22.0  没戴眼睛  0.0  0.001  0.0  0.0   \n",
       "d5099372-010d-4d2d-9a65-143775e18684  男性  22.0  没戴眼睛  0.0  0.000  0.0  0.0   \n",
       "3331f6cc-b137-48e0-b174-2987a5d357ee  男性  21.0  没戴眼睛  0.0  0.000  0.0  0.0   \n",
       "b19d1a74-ec12-4536-b67f-a3dab1e8e4b2  男性  20.0  没戴眼睛  0.0  0.000  0.0  0.0   \n",
       "\n",
       "                                         高兴     平静   伤心     惊讶  \n",
       "faceId                                                          \n",
       "a0934bcf-e4fd-435e-b9d7-8f1e4b8c0dc3  0.810  0.187  0.0  0.002  \n",
       "d5099372-010d-4d2d-9a65-143775e18684  0.985  0.000  0.0  0.014  \n",
       "3331f6cc-b137-48e0-b174-2987a5d357ee  0.980  0.000  0.0  0.019  \n",
       "b19d1a74-ec12-4536-b67f-a3dab1e8e4b2  1.000  0.000  0.0  0.000  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd #导入pandas库 \n",
    "df_ax = pd.json_normalize(results)# 升级pandas才能运行\n",
    "df_ax = df_ax.rename ( columns = {\"faceAttributes.gender\":\"性别\", \n",
    "                       \"faceAttributes.age\":\"年龄\",\n",
    "                       \"faceAttributes.glasses\":\"眼镜\",\n",
    "                       \"faceAttributes.emotion.anger\":\"生气\",\n",
    "                       \"faceAttributes.emotion.contempt\":\"蔑视\",\n",
    "                       \"faceAttributes.emotion.disgust\":\"厌恶\",\n",
    "                       \"faceAttributes.emotion.fear\":\"恐惧\",\n",
    "                       \"faceAttributes.emotion.happiness\":\"高兴\",\n",
    "                       \"faceAttributes.emotion.neutral\":\"平静\",\n",
    "                       \"faceAttributes.emotion.sadness\":\"伤心\",\n",
    "                       \"faceAttributes.emotion.surprise\":\"惊讶\",} )\n",
    "df_ax = df_ax.set_index('faceId')\n",
    "df_ax = df_ax.iloc[:,4:]\n",
    "df_ax.replace({\"male\":\"男性\",\n",
    "               \"female\":\"女性\",\n",
    "              \"NoGlasses\":\"没戴眼睛\",\n",
    "              \"ReadingGlasses\":\"戴眼镜\",})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 成功了，再次尝试第2次，选择一张单人图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '0ae94ab5-29d7-4c61-b17f-80a8f00d5443',\n",
       "  'faceRectangle': {'top': 173, 'left': 445, 'width': 235, 'height': 235},\n",
       "  'faceAttributes': {'gender': 'male',\n",
       "   'age': 19.0,\n",
       "   'glasses': 'ReadingGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 1.0,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0}}}]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先导入为们需要的模块\n",
    "import requests\n",
    "import json\n",
    "KEY = 'e969c474c0df4357a1f2a9dbf3b2329a'  # 输入自己的KEY值\n",
    "BASE_URL = 'https://zoe-apiface.cognitiveservices.azure.com/face/v1.0/detect' #根据自己组成后的终极地址填入人脸识别的api地址\n",
    "\n",
    "# 沿用API的示范代碼，{subscription key}用KEY代入\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY),\n",
    "}\n",
    "\n",
    "img_url = 'http://p7.qhimg.com/bdm/1000_618_80/t01579e5d1cb540b136.jpg' # 一张易烊千玺戴眼镜的照片\n",
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses,emotion'), #年龄、性别、眼镜、情感\n",
    "}\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)#HTTP post请求 请求参数\n",
    "\n",
    "r.status_code#查看参数回传状态码\n",
    "results = r.json() #将回传数据转化为json格式\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>眼镜</th>\n",
       "      <th>生气</th>\n",
       "      <th>蔑视</th>\n",
       "      <th>厌恶</th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>faceId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0ae94ab5-29d7-4c61-b17f-80a8f00d5443</th>\n",
       "      <td>男性</td>\n",
       "      <td>19.0</td>\n",
       "      <td>戴眼镜</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      性别    年龄   眼镜   生气   蔑视   厌恶   恐惧   高兴  \\\n",
       "faceId                                                                         \n",
       "0ae94ab5-29d7-4c61-b17f-80a8f00d5443  男性  19.0  戴眼镜  0.0  0.0  0.0  0.0  1.0   \n",
       "\n",
       "                                       平静   伤心   惊讶  \n",
       "faceId                                               \n",
       "0ae94ab5-29d7-4c61-b17f-80a8f00d5443  0.0  0.0  0.0  "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd #导入pandas库 \n",
    "df_ax = pd.json_normalize(results)# 升级pandas才能运行\n",
    "df_ax = df_ax.rename ( columns = {\"faceAttributes.gender\":\"性别\", \n",
    "                       \"faceAttributes.age\":\"年龄\",\n",
    "                       \"faceAttributes.glasses\":\"眼镜\",\n",
    "                       \"faceAttributes.emotion.anger\":\"生气\",\n",
    "                       \"faceAttributes.emotion.contempt\":\"蔑视\",\n",
    "                       \"faceAttributes.emotion.disgust\":\"厌恶\",\n",
    "                       \"faceAttributes.emotion.fear\":\"恐惧\",\n",
    "                       \"faceAttributes.emotion.happiness\":\"高兴\",\n",
    "                       \"faceAttributes.emotion.neutral\":\"平静\",\n",
    "                       \"faceAttributes.emotion.sadness\":\"伤心\",\n",
    "                       \"faceAttributes.emotion.surprise\":\"惊讶\",} )\n",
    "df_ax = df_ax.set_index('faceId')\n",
    "df_ax = df_ax.iloc[:,4:]\n",
    "df_ax.replace({\"male\":\"男性\",\n",
    "               \"female\":\"女性\",\n",
    "              \"NoGlasses\":\"没戴眼睛\",\n",
    "              \"ReadingGlasses\":\"戴眼镜\",})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第3张图，多人，有男有女"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'ab62d664-068a-4129-8bb9-1e996e5d5f99',\n",
       "  'faceRectangle': {'top': 1795, 'left': 3147, 'width': 449, 'height': 449},\n",
       "  'faceAttributes': {'gender': 'male',\n",
       "   'age': 33.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.627,\n",
       "    'neutral': 0.372,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0}}},\n",
       " {'faceId': '7c245e92-753d-43b2-a609-f4e19a051d44',\n",
       "  'faceRectangle': {'top': 2507, 'left': 5115, 'width': 382, 'height': 382},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 30.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.002,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.645,\n",
       "    'neutral': 0.346,\n",
       "    'sadness': 0.007,\n",
       "    'surprise': 0.0}}},\n",
       " {'faceId': 'a5e0dc30-7117-427c-92be-0e718261fb9e',\n",
       "  'faceRectangle': {'top': 1875, 'left': 4022, 'width': 352, 'height': 352},\n",
       "  'faceAttributes': {'gender': 'male',\n",
       "   'age': 25.0,\n",
       "   'glasses': 'Sunglasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 1.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0}}},\n",
       " {'faceId': '15c50199-bb91-4cdd-91ff-da4e87604762',\n",
       "  'faceRectangle': {'top': 865, 'left': 2465, 'width': 288, 'height': 288},\n",
       "  'faceAttributes': {'gender': 'male',\n",
       "   'age': 23.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 1.0,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0}}},\n",
       " {'faceId': '81fca026-5400-4b55-825d-7bfc8a91b0d6',\n",
       "  'faceRectangle': {'top': 961, 'left': 2208, 'width': 219, 'height': 219},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 29.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.84,\n",
       "    'neutral': 0.102,\n",
       "    'sadness': 0.003,\n",
       "    'surprise': 0.055}}}]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先导入为们需要的模块\n",
    "import requests\n",
    "import json\n",
    "KEY = 'e969c474c0df4357a1f2a9dbf3b2329a'  # 输入自己的KEY值\n",
    "BASE_URL = 'https://zoe-apiface.cognitiveservices.azure.com/face/v1.0/detect' #根据自己组成后的终极地址填入人脸识别的api地址\n",
    "\n",
    "# 沿用API的示范代碼，{subscription key}用KEY代入\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY),\n",
    "}\n",
    "\n",
    "img_url = 'http://xbzmzl-yun.oss-cn-shenzhen.aliyuncs.com/Uploads/WallUploads/v2/45/83e54c48d8984df5c88e6f7a69d233c5.jpg' # 一张混合多人的照片\n",
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses,emotion'), #年龄、性别、眼镜、情感\n",
    "}\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)#HTTP post请求 请求参数\n",
    "\n",
    "r.status_code#查看参数回传状态码\n",
    "results = r.json() #将回传数据转化为json格式\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>年龄</th>\n",
       "      <th>眼镜</th>\n",
       "      <th>生气</th>\n",
       "      <th>蔑视</th>\n",
       "      <th>厌恶</th>\n",
       "      <th>恐惧</th>\n",
       "      <th>高兴</th>\n",
       "      <th>平静</th>\n",
       "      <th>伤心</th>\n",
       "      <th>惊讶</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>faceId</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ab62d664-068a-4129-8bb9-1e996e5d5f99</th>\n",
       "      <td>男性</td>\n",
       "      <td>33.0</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.627</td>\n",
       "      <td>0.372</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7c245e92-753d-43b2-a609-f4e19a051d44</th>\n",
       "      <td>女性</td>\n",
       "      <td>30.0</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.645</td>\n",
       "      <td>0.346</td>\n",
       "      <td>0.007</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a5e0dc30-7117-427c-92be-0e718261fb9e</th>\n",
       "      <td>男性</td>\n",
       "      <td>25.0</td>\n",
       "      <td>Sunglasses</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15c50199-bb91-4cdd-91ff-da4e87604762</th>\n",
       "      <td>男性</td>\n",
       "      <td>23.0</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>81fca026-5400-4b55-825d-7bfc8a91b0d6</th>\n",
       "      <td>女性</td>\n",
       "      <td>29.0</td>\n",
       "      <td>没戴眼睛</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.840</td>\n",
       "      <td>0.102</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.055</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      性别    年龄          眼镜   生气     蔑视   厌恶  \\\n",
       "faceId                                                                        \n",
       "ab62d664-068a-4129-8bb9-1e996e5d5f99  男性  33.0        没戴眼睛  0.0  0.000  0.0   \n",
       "7c245e92-753d-43b2-a609-f4e19a051d44  女性  30.0        没戴眼睛  0.0  0.002  0.0   \n",
       "a5e0dc30-7117-427c-92be-0e718261fb9e  男性  25.0  Sunglasses  0.0  0.000  0.0   \n",
       "15c50199-bb91-4cdd-91ff-da4e87604762  男性  23.0        没戴眼睛  0.0  0.000  0.0   \n",
       "81fca026-5400-4b55-825d-7bfc8a91b0d6  女性  29.0        没戴眼睛  0.0  0.000  0.0   \n",
       "\n",
       "                                       恐惧     高兴     平静     伤心     惊讶  \n",
       "faceId                                                                 \n",
       "ab62d664-068a-4129-8bb9-1e996e5d5f99  0.0  0.627  0.372  0.000  0.000  \n",
       "7c245e92-753d-43b2-a609-f4e19a051d44  0.0  0.645  0.346  0.007  0.000  \n",
       "a5e0dc30-7117-427c-92be-0e718261fb9e  0.0  0.000  1.000  0.000  0.000  \n",
       "15c50199-bb91-4cdd-91ff-da4e87604762  0.0  1.000  0.000  0.000  0.000  \n",
       "81fca026-5400-4b55-825d-7bfc8a91b0d6  0.0  0.840  0.102  0.003  0.055  "
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd #导入pandas库 \n",
    "df_ax = pd.json_normalize(results)# 升级pandas才能运行\n",
    "df_ax = df_ax.rename ( columns = {\"faceAttributes.gender\":\"性别\", \n",
    "                       \"faceAttributes.age\":\"年龄\",\n",
    "                       \"faceAttributes.glasses\":\"眼镜\",\n",
    "                       \"faceAttributes.emotion.anger\":\"生气\",\n",
    "                       \"faceAttributes.emotion.contempt\":\"蔑视\",\n",
    "                       \"faceAttributes.emotion.disgust\":\"厌恶\",\n",
    "                       \"faceAttributes.emotion.fear\":\"恐惧\",\n",
    "                       \"faceAttributes.emotion.happiness\":\"高兴\",\n",
    "                       \"faceAttributes.emotion.neutral\":\"平静\",\n",
    "                       \"faceAttributes.emotion.sadness\":\"伤心\",\n",
    "                       \"faceAttributes.emotion.surprise\":\"惊讶\",} )\n",
    "df_ax = df_ax.set_index('faceId')\n",
    "df_ax = df_ax.iloc[:,4:]\n",
    "df_ax.replace({\"male\":\"男性\",\n",
    "               \"female\":\"女性\",\n",
    "              \"NoGlasses\":\"没戴眼睛\",\n",
    "              \"ReadingGlasses\":\"戴眼镜\",})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 多人混合这张图，人好多，但它只识别出五个人？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "#  face++ Detect API(面部检测) \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第1个图片尝试，多人图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1586931330,2187de1b-c176-4b3d-9a0e-70b281a5817e',\n",
       " 'time_used': 342,\n",
       " 'faces': [{'face_token': '8573e8fcee061f0cd8417c9301236be0',\n",
       "   'face_rectangle': {'top': 106, 'left': 418, 'width': 62, 'height': 62},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 26},\n",
       "    'smile': {'value': 99.854, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.134,\n",
       "     'disgust': 0.069,\n",
       "     'fear': 56.864,\n",
       "     'happiness': 40.443,\n",
       "     'neutral': 0.829,\n",
       "     'sadness': 1.231,\n",
       "     'surprise': 0.43}}},\n",
       "  {'face_token': 'ba37746466dfea502ec28f1401f064e5',\n",
       "   'face_rectangle': {'top': 172, 'left': 147, 'width': 62, 'height': 62},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 27},\n",
       "    'smile': {'value': 99.298, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.082,\n",
       "     'disgust': 0.154,\n",
       "     'fear': 0.081,\n",
       "     'happiness': 85.324,\n",
       "     'neutral': 0.763,\n",
       "     'sadness': 0.24,\n",
       "     'surprise': 13.357}}},\n",
       "  {'face_token': 'acce82f5fa5e6279d9d7a5d802e59b7c',\n",
       "   'face_rectangle': {'top': 178, 'left': 293, 'width': 61, 'height': 61},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 41},\n",
       "    'smile': {'value': 99.922, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.038,\n",
       "     'disgust': 0.038,\n",
       "     'fear': 0.09,\n",
       "     'happiness': 91.225,\n",
       "     'neutral': 0.08,\n",
       "     'sadness': 7.916,\n",
       "     'surprise': 0.613}}},\n",
       "  {'face_token': '7085a9d7bdd81b58492a9a232d5c50bc',\n",
       "   'face_rectangle': {'top': 88, 'left': 574, 'width': 57, 'height': 57},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 32},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.004,\n",
       "     'disgust': 0.24,\n",
       "     'fear': 0.001,\n",
       "     'happiness': 99.749,\n",
       "     'neutral': 0.003,\n",
       "     'sadness': 0.002,\n",
       "     'surprise': 0.001}}}],\n",
       " 'image_id': 'jw2PfBJUVdn9qrm3y9cfHA==',\n",
       " 'face_num': 4}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "\n",
    "\n",
    "api_secret = \"KgZWE35Sr3AfTNeguHucCj7J2TqGJvBT\"\n",
    "# 2、输入我们API_Key\n",
    "api_key = 'jbihCMcQ1xbkKYqIGQ14VujMgS3KzYe1'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'http://picm.bbzhi.com/mingxingbizhi/feilunhaifeilunhaibizhi/star_starhk_141010_m.jpg' # 还是那张飞轮海年轻照片\n",
    "\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "# print(r.content)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 要如何简化此数据方便使用?\n",
    "* pandas !!\n",
    "* json_normalize：\n",
    "    * [json_normalize github](https://github.com/pandas-dev/pandas/blob/v1.0.3/pandas/io/json/_normalize.py#L114-L358)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  face_rectangle (json_normalize方法) pandas黑魔法\n",
    "* 先针对这变量看观察"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "# from pandas.io.json import json_normalize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# results['faces']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "df = pd.json_normalize(results,record_path='faces')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <th>attributes.age.value</th>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>8573e8fcee061f0cd8417c9301236be0</td>\n",
       "      <td>106</td>\n",
       "      <td>418</td>\n",
       "      <td>62</td>\n",
       "      <td>62</td>\n",
       "      <td>Female</td>\n",
       "      <td>26</td>\n",
       "      <td>99.854</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.134</td>\n",
       "      <td>0.069</td>\n",
       "      <td>56.864</td>\n",
       "      <td>40.443</td>\n",
       "      <td>0.829</td>\n",
       "      <td>1.231</td>\n",
       "      <td>0.430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ba37746466dfea502ec28f1401f064e5</td>\n",
       "      <td>172</td>\n",
       "      <td>147</td>\n",
       "      <td>62</td>\n",
       "      <td>62</td>\n",
       "      <td>Female</td>\n",
       "      <td>27</td>\n",
       "      <td>99.298</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.082</td>\n",
       "      <td>0.154</td>\n",
       "      <td>0.081</td>\n",
       "      <td>85.324</td>\n",
       "      <td>0.763</td>\n",
       "      <td>0.240</td>\n",
       "      <td>13.357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>acce82f5fa5e6279d9d7a5d802e59b7c</td>\n",
       "      <td>178</td>\n",
       "      <td>293</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>Female</td>\n",
       "      <td>41</td>\n",
       "      <td>99.922</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.038</td>\n",
       "      <td>0.038</td>\n",
       "      <td>0.090</td>\n",
       "      <td>91.225</td>\n",
       "      <td>0.080</td>\n",
       "      <td>7.916</td>\n",
       "      <td>0.613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7085a9d7bdd81b58492a9a232d5c50bc</td>\n",
       "      <td>88</td>\n",
       "      <td>574</td>\n",
       "      <td>57</td>\n",
       "      <td>57</td>\n",
       "      <td>Female</td>\n",
       "      <td>32</td>\n",
       "      <td>100.000</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.004</td>\n",
       "      <td>0.240</td>\n",
       "      <td>0.001</td>\n",
       "      <td>99.749</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  face_rectangle.top  face_rectangle.left  \\\n",
       "0  8573e8fcee061f0cd8417c9301236be0                 106                  418   \n",
       "1  ba37746466dfea502ec28f1401f064e5                 172                  147   \n",
       "2  acce82f5fa5e6279d9d7a5d802e59b7c                 178                  293   \n",
       "3  7085a9d7bdd81b58492a9a232d5c50bc                  88                  574   \n",
       "\n",
       "   face_rectangle.width  face_rectangle.height attributes.gender.value  \\\n",
       "0                    62                     62                  Female   \n",
       "1                    62                     62                  Female   \n",
       "2                    61                     61                  Female   \n",
       "3                    57                     57                  Female   \n",
       "\n",
       "   attributes.age.value  attributes.smile.value  attributes.smile.threshold  \\\n",
       "0                    26                  99.854                        50.0   \n",
       "1                    27                  99.298                        50.0   \n",
       "2                    41                  99.922                        50.0   \n",
       "3                    32                 100.000                        50.0   \n",
       "\n",
       "   attributes.emotion.anger  attributes.emotion.disgust  \\\n",
       "0                     0.134                       0.069   \n",
       "1                     0.082                       0.154   \n",
       "2                     0.038                       0.038   \n",
       "3                     0.004                       0.240   \n",
       "\n",
       "   attributes.emotion.fear  attributes.emotion.happiness  \\\n",
       "0                   56.864                        40.443   \n",
       "1                    0.081                        85.324   \n",
       "2                    0.090                        91.225   \n",
       "3                    0.001                        99.749   \n",
       "\n",
       "   attributes.emotion.neutral  attributes.emotion.sadness  \\\n",
       "0                       0.829                       1.231   \n",
       "1                       0.763                       0.240   \n",
       "2                       0.080                       7.916   \n",
       "3                       0.003                       0.002   \n",
       "\n",
       "   attributes.emotion.surprise  \n",
       "0                        0.430  \n",
       "1                       13.357  \n",
       "2                        0.613  \n",
       "3                        0.001  "
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第2张单人图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1586932888,65642d0e-02b1-443e-a4e1-ab2adda00dac',\n",
       " 'time_used': 1419,\n",
       " 'faces': [{'face_token': '94ef58445d00ba2135f35838cbce6a8c',\n",
       "   'face_rectangle': {'top': 191, 'left': 428, 'width': 244, 'height': 244},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 22},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.012,\n",
       "     'disgust': 10.139,\n",
       "     'fear': 0.189,\n",
       "     'happiness': 89.108,\n",
       "     'neutral': 0.012,\n",
       "     'sadness': 0.529,\n",
       "     'surprise': 0.012}}}],\n",
       " 'image_id': 'gAFDrxhzref8QNajCufzKQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "\n",
    "\n",
    "api_secret = \"KgZWE35Sr3AfTNeguHucCj7J2TqGJvBT\"\n",
    "# 2、输入我们API_Key\n",
    "api_key = 'jbihCMcQ1xbkKYqIGQ14VujMgS3KzYe1'  \n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'http://p7.qhimg.com/bdm/1000_618_80/t01579e5d1cb540b136.jpg'  # 一张易烊千玺戴眼镜的照片\n",
    "\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用Pandas表格化数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <th>attributes.age.value</th>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>94ef58445d00ba2135f35838cbce6a8c</td>\n",
       "      <td>191</td>\n",
       "      <td>428</td>\n",
       "      <td>244</td>\n",
       "      <td>244</td>\n",
       "      <td>Male</td>\n",
       "      <td>22</td>\n",
       "      <td>100.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.012</td>\n",
       "      <td>10.139</td>\n",
       "      <td>0.189</td>\n",
       "      <td>89.108</td>\n",
       "      <td>0.012</td>\n",
       "      <td>0.529</td>\n",
       "      <td>0.012</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  face_rectangle.top  face_rectangle.left  \\\n",
       "0  94ef58445d00ba2135f35838cbce6a8c                 191                  428   \n",
       "\n",
       "   face_rectangle.width  face_rectangle.height attributes.gender.value  \\\n",
       "0                   244                    244                    Male   \n",
       "\n",
       "   attributes.age.value  attributes.smile.value  attributes.smile.threshold  \\\n",
       "0                    22                   100.0                        50.0   \n",
       "\n",
       "   attributes.emotion.anger  attributes.emotion.disgust  \\\n",
       "0                     0.012                      10.139   \n",
       "\n",
       "   attributes.emotion.fear  attributes.emotion.happiness  \\\n",
       "0                    0.189                        89.108   \n",
       "\n",
       "   attributes.emotion.neutral  attributes.emotion.sadness  \\\n",
       "0                       0.012                       0.529   \n",
       "\n",
       "   attributes.emotion.surprise  \n",
       "0                        0.012  "
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results\n",
    "\n",
    "# 使用Pandas，将上面的数据表格化呈现\n",
    "face_pd = pd.json_normalize(results,record_path='faces')\n",
    "face_pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第3张图，多人男女"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1586934015,88c1002c-5dcb-437f-9222-af6773b949a4',\n",
       " 'time_used': 733,\n",
       " 'faces': [{'face_token': 'b4d5218ff3ab5b8c372693d6b383b70c',\n",
       "   'face_rectangle': {'top': 65, 'left': 149, 'width': 37, 'height': 37},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 28},\n",
       "    'smile': {'value': 0.008, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 1.32,\n",
       "     'disgust': 0.004,\n",
       "     'fear': 0.003,\n",
       "     'happiness': 0.003,\n",
       "     'neutral': 98.645,\n",
       "     'sadness': 0.003,\n",
       "     'surprise': 0.023}}},\n",
       "  {'face_token': '78ff16fe128185df108793a548c984a1',\n",
       "   'face_rectangle': {'top': 63, 'left': 274, 'width': 37, 'height': 37},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 29},\n",
       "    'smile': {'value': 0.441, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 1.926,\n",
       "     'disgust': 5.82,\n",
       "     'fear': 0.22,\n",
       "     'happiness': 0.14,\n",
       "     'neutral': 89.392,\n",
       "     'sadness': 2.38,\n",
       "     'surprise': 0.122}}},\n",
       "  {'face_token': '892cae380b942e03087126a7cd5a5074',\n",
       "   'face_rectangle': {'top': 80, 'left': 213, 'width': 35, 'height': 35},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 23},\n",
       "    'smile': {'value': 0.002, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.012,\n",
       "     'disgust': 0.002,\n",
       "     'fear': 0.002,\n",
       "     'happiness': 0.01,\n",
       "     'neutral': 99.956,\n",
       "     'sadness': 0.017,\n",
       "     'surprise': 0.002}}},\n",
       "  {'face_token': '1b3573b0f679e0cc5ac4f76135124980',\n",
       "   'face_rectangle': {'top': 92, 'left': 51, 'width': 24, 'height': 24},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 42},\n",
       "    'smile': {'value': 0.008, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.0,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 0.001,\n",
       "     'neutral': 99.987,\n",
       "     'sadness': 0.01,\n",
       "     'surprise': 0.002}}},\n",
       "  {'face_token': 'dd41a98df221d8f16dfb317449990bf3',\n",
       "   'face_rectangle': {'top': 62, 'left': 337, 'width': 23, 'height': 23},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 32},\n",
       "    'smile': {'value': 1.495, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 23.483,\n",
       "     'disgust': 11.176,\n",
       "     'fear': 0.029,\n",
       "     'happiness': 0.029,\n",
       "     'neutral': 65.111,\n",
       "     'sadness': 0.144,\n",
       "     'surprise': 0.029}}},\n",
       "  {'face_token': '6d13c1b0658af10980acb49dcbd6f452',\n",
       "   'face_rectangle': {'top': 62, 'left': 416, 'width': 23, 'height': 23}},\n",
       "  {'face_token': '7277d0bf56f7e740639eb3151da303e3',\n",
       "   'face_rectangle': {'top': 84, 'left': 373, 'width': 22, 'height': 22}},\n",
       "  {'face_token': 'c607fd6b3aaec32098ccd7df56784590',\n",
       "   'face_rectangle': {'top': 77, 'left': 113, 'width': 21, 'height': 21}}],\n",
       " 'image_id': 'HmiTMFT4Nzm6Yfh7ZpDlnA==',\n",
       " 'face_num': 8}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "\n",
    "\n",
    "api_secret = \"KgZWE35Sr3AfTNeguHucCj7J2TqGJvBT\"\n",
    "# 2、输入我们API_Key\n",
    "api_key = 'jbihCMcQ1xbkKYqIGQ14VujMgS3KzYe1'  \n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://tse3-mm.cn.bing.net/th?id=OIP.KyZpeg5xqp7TuPWhW9jZrwHaEo&pid=Api&rs=1' # 一张混合多人的照片，暮光之城\n",
    "\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用Pandas表格化数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <th>attributes.age.value</th>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b4d5218ff3ab5b8c372693d6b383b70c</td>\n",
       "      <td>65</td>\n",
       "      <td>149</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>Male</td>\n",
       "      <td>28.0</td>\n",
       "      <td>0.008</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1.320</td>\n",
       "      <td>0.004</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.003</td>\n",
       "      <td>98.645</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>78ff16fe128185df108793a548c984a1</td>\n",
       "      <td>63</td>\n",
       "      <td>274</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>Male</td>\n",
       "      <td>29.0</td>\n",
       "      <td>0.441</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1.926</td>\n",
       "      <td>5.820</td>\n",
       "      <td>0.220</td>\n",
       "      <td>0.140</td>\n",
       "      <td>89.392</td>\n",
       "      <td>2.380</td>\n",
       "      <td>0.122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>892cae380b942e03087126a7cd5a5074</td>\n",
       "      <td>80</td>\n",
       "      <td>213</td>\n",
       "      <td>35</td>\n",
       "      <td>35</td>\n",
       "      <td>Female</td>\n",
       "      <td>23.0</td>\n",
       "      <td>0.002</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.012</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.010</td>\n",
       "      <td>99.956</td>\n",
       "      <td>0.017</td>\n",
       "      <td>0.002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1b3573b0f679e0cc5ac4f76135124980</td>\n",
       "      <td>92</td>\n",
       "      <td>51</td>\n",
       "      <td>24</td>\n",
       "      <td>24</td>\n",
       "      <td>Female</td>\n",
       "      <td>42.0</td>\n",
       "      <td>0.008</td>\n",
       "      <td>50.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.001</td>\n",
       "      <td>99.987</td>\n",
       "      <td>0.010</td>\n",
       "      <td>0.002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>dd41a98df221d8f16dfb317449990bf3</td>\n",
       "      <td>62</td>\n",
       "      <td>337</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>Male</td>\n",
       "      <td>32.0</td>\n",
       "      <td>1.495</td>\n",
       "      <td>50.0</td>\n",
       "      <td>23.483</td>\n",
       "      <td>11.176</td>\n",
       "      <td>0.029</td>\n",
       "      <td>0.029</td>\n",
       "      <td>65.111</td>\n",
       "      <td>0.144</td>\n",
       "      <td>0.029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6d13c1b0658af10980acb49dcbd6f452</td>\n",
       "      <td>62</td>\n",
       "      <td>416</td>\n",
       "      <td>23</td>\n",
       "      <td>23</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7277d0bf56f7e740639eb3151da303e3</td>\n",
       "      <td>84</td>\n",
       "      <td>373</td>\n",
       "      <td>22</td>\n",
       "      <td>22</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>c607fd6b3aaec32098ccd7df56784590</td>\n",
       "      <td>77</td>\n",
       "      <td>113</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  face_rectangle.top  face_rectangle.left  \\\n",
       "0  b4d5218ff3ab5b8c372693d6b383b70c                  65                  149   \n",
       "1  78ff16fe128185df108793a548c984a1                  63                  274   \n",
       "2  892cae380b942e03087126a7cd5a5074                  80                  213   \n",
       "3  1b3573b0f679e0cc5ac4f76135124980                  92                   51   \n",
       "4  dd41a98df221d8f16dfb317449990bf3                  62                  337   \n",
       "5  6d13c1b0658af10980acb49dcbd6f452                  62                  416   \n",
       "6  7277d0bf56f7e740639eb3151da303e3                  84                  373   \n",
       "7  c607fd6b3aaec32098ccd7df56784590                  77                  113   \n",
       "\n",
       "   face_rectangle.width  face_rectangle.height attributes.gender.value  \\\n",
       "0                    37                     37                    Male   \n",
       "1                    37                     37                    Male   \n",
       "2                    35                     35                  Female   \n",
       "3                    24                     24                  Female   \n",
       "4                    23                     23                    Male   \n",
       "5                    23                     23                     NaN   \n",
       "6                    22                     22                     NaN   \n",
       "7                    21                     21                     NaN   \n",
       "\n",
       "   attributes.age.value  attributes.smile.value  attributes.smile.threshold  \\\n",
       "0                  28.0                   0.008                        50.0   \n",
       "1                  29.0                   0.441                        50.0   \n",
       "2                  23.0                   0.002                        50.0   \n",
       "3                  42.0                   0.008                        50.0   \n",
       "4                  32.0                   1.495                        50.0   \n",
       "5                   NaN                     NaN                         NaN   \n",
       "6                   NaN                     NaN                         NaN   \n",
       "7                   NaN                     NaN                         NaN   \n",
       "\n",
       "   attributes.emotion.anger  attributes.emotion.disgust  \\\n",
       "0                     1.320                       0.004   \n",
       "1                     1.926                       5.820   \n",
       "2                     0.012                       0.002   \n",
       "3                     0.000                       0.000   \n",
       "4                    23.483                      11.176   \n",
       "5                       NaN                         NaN   \n",
       "6                       NaN                         NaN   \n",
       "7                       NaN                         NaN   \n",
       "\n",
       "   attributes.emotion.fear  attributes.emotion.happiness  \\\n",
       "0                    0.003                         0.003   \n",
       "1                    0.220                         0.140   \n",
       "2                    0.002                         0.010   \n",
       "3                    0.000                         0.001   \n",
       "4                    0.029                         0.029   \n",
       "5                      NaN                           NaN   \n",
       "6                      NaN                           NaN   \n",
       "7                      NaN                           NaN   \n",
       "\n",
       "   attributes.emotion.neutral  attributes.emotion.sadness  \\\n",
       "0                      98.645                       0.003   \n",
       "1                      89.392                       2.380   \n",
       "2                      99.956                       0.017   \n",
       "3                      99.987                       0.010   \n",
       "4                      65.111                       0.144   \n",
       "5                         NaN                         NaN   \n",
       "6                         NaN                         NaN   \n",
       "7                         NaN                         NaN   \n",
       "\n",
       "   attributes.emotion.surprise  \n",
       "0                        0.023  \n",
       "1                        0.122  \n",
       "2                        0.002  \n",
       "3                        0.002  \n",
       "4                        0.029  \n",
       "5                          NaN  \n",
       "6                          NaN  \n",
       "7                          NaN  "
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results\n",
    "\n",
    "# 使用Pandas，将上面的数据表格化呈现\n",
    "face_pd = pd.json_normalize(results,record_path='faces')\n",
    "face_pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 它这里识别多人也会有误差，9人只识别出7人？\n",
    "+ 回过头来，用Azure来识别这张暮光之城的多人图片，只识别出3人？？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 百度AI开放平台，人脸识别（人脸检测与属性分析）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.5a7bc64c9fb5dd6fbda48a8a7c35c700.315360000.1902320732.282335-19446424', 'expires_in': 2592000, 'session_key': '9mzdCPe7yuIdnGviwd3lCOa2v8JpcrH9YrDRUdRj6tHWkqiHUQoZsUhHn1BZBzrQuUWPPmAq9EdAuolGPsDb43EZ4NYT2g==', 'access_token': '24.3371f71f929af4c84754c8c39409d013.2592000.1589552732.282335-19446424', 'scope': 'public brain_all_scope vis-classify_实时检索-相似 brain_realtime_same_hq brain_realtime_similar vis-faceverify_faceverify_h5-face-liveness brain_body_analysis vis-faceverify_FACE_V3 brain_body_seg vis-faceverify_idl_face_merge wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi qatest_scope1 fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理', 'session_secret': 'cbd5462b6c6e821775ed42414539849a'}\n"
     ]
    }
   ],
   "source": [
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "# client_id = ‘RiEGNLSVssc2ueGIxWmM6PVQ’\n",
    "# client_secret = ‘OurCAa1ZwORwwMaSXq9oGkTcqGPpTB2D’\n",
    "\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=RiEGNLSVssc2ueGIxWmM6PVQ&client_secret=OurCAa1ZwORwwMaSXq9oGkTcqGPpTB2D'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 尝试一张单人图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 9920165001059, 'timestamp': 1586961957, 'cached': 0, 'result': {'face_num': 1, 'face_list': [{'face_token': 'd28910d301be1de7d1202c6e22020343', 'location': {'left': 413.96, 'top': 205.78, 'width': 248, 'height': 228, 'rotation': -2}, 'face_probability': 1, 'angle': {'yaw': -15.91, 'pitch': 21.17, 'roll': 1.25}, 'face_shape': {'type': 'oval', 'probability': 0.59}, 'age': 23, 'emotion': {'type': 'happy', 'probability': 1}}]}}\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>error_code</th>\n",
       "      <th>error_msg</th>\n",
       "      <th>log_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>cached</th>\n",
       "      <th>result.face_num</th>\n",
       "      <th>result.face_list</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>SUCCESS</td>\n",
       "      <td>9920165001059</td>\n",
       "      <td>1586961957</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>[{'face_token': 'd28910d301be1de7d1202c6e22020...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   error_code error_msg         log_id   timestamp  cached  result.face_num  \\\n",
       "0           0   SUCCESS  9920165001059  1586961957       0                1   \n",
       "\n",
       "                                    result.face_list  \n",
       "0  [{'face_token': 'd28910d301be1de7d1202c6e22020...  "
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "'''\n",
    "人脸检测与属性分析\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "#选择一张单人照\n",
    "params = {\"image\":\"http://p7.qhimg.com/bdm/1000_618_80/t01579e5d1cb540b136.jpg\",\n",
    "          # 一张易烊千玺戴眼镜的照片\n",
    "          \"image_type\":\"URL\",\n",
    "          \"face_field\":\"faceshape,age,emotion\"\n",
    "}\n",
    "\n",
    "access_token = \"24.6518b428b7e4cd6f0b698d0e92b8f674.2592000.1588350394.282335-19207134\"\n",
    "request_url = request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {\n",
    "    'content-type': 'application/json',\n",
    "}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "\n",
    "response.status_code\n",
    "\n",
    "baidu_r =response.json()\n",
    "baidu_r\n",
    "\n",
    "baidu_df = pd.json_normalize(baidu_r)\n",
    "baidu_df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一张多人照（4人）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 8494152500165, 'timestamp': 1586961446, 'cached': 0, 'result': {'face_num': 1, 'face_list': [{'face_token': '9ac1c4f9d2c54e9677e033c593eb83cf', 'location': {'left': 151.41, 'top': 170.98, 'width': 61, 'height': 61, 'rotation': 5}, 'face_probability': 1, 'angle': {'yaw': 8.14, 'pitch': 11.64, 'roll': -1.41}, 'face_shape': {'type': 'oval', 'probability': 0.61}, 'age': 22, 'emotion': {'type': 'happy', 'probability': 1}}]}}\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>error_code</th>\n",
       "      <th>error_msg</th>\n",
       "      <th>log_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>cached</th>\n",
       "      <th>result.face_num</th>\n",
       "      <th>result.face_list</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>SUCCESS</td>\n",
       "      <td>8494152500165</td>\n",
       "      <td>1586961446</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>[{'face_token': '9ac1c4f9d2c54e9677e033c593eb8...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   error_code error_msg         log_id   timestamp  cached  result.face_num  \\\n",
       "0           0   SUCCESS  8494152500165  1586961446       0                1   \n",
       "\n",
       "                                    result.face_list  \n",
       "0  [{'face_token': '9ac1c4f9d2c54e9677e033c593eb8...  "
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选择四人照\n",
    "params = {\"image\":\"http://picm.bbzhi.com/mingxingbizhi/feilunhaifeilunhaibizhi/star_starhk_141010_m.jpg\",\n",
    "          # 飞轮海，4人\n",
    "          \"image_type\":\"URL\",\n",
    "          \"face_field\":\"faceshape,age,emotion\"\n",
    "}\n",
    "response2 = requests.post(request_url, data=params, headers=headers)\n",
    "baidu_r2 =response2.json()\n",
    "print(baidu_r2)\n",
    "\n",
    "baidu_df2 = pd.json_normalize(baidu_r2)\n",
    "baidu_df2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一张多人照"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 222304, 'error_msg': 'image size is too large', 'log_id': 575201201058, 'timestamp': 1586961547, 'cached': 0, 'result': None}\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>error_code</th>\n",
       "      <th>error_msg</th>\n",
       "      <th>log_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>cached</th>\n",
       "      <th>result</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>222304</td>\n",
       "      <td>image size is too large</td>\n",
       "      <td>575201201058</td>\n",
       "      <td>1586961547</td>\n",
       "      <td>0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   error_code                error_msg        log_id   timestamp  cached  \\\n",
       "0      222304  image size is too large  575201201058  1586961547       0   \n",
       "\n",
       "  result  \n",
       "0   None  "
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一张混合多人的照片\n",
    "params = {\"image\":\"http://xbzmzl-yun.oss-cn-shenzhen.aliyuncs.com/Uploads/WallUploads/v2/45/83e54c48d8984df5c88e6f7a69d233c5.jpg\",\n",
    "          \"image_type\":\"URL\",\n",
    "          \"face_field\":\"faceshape,age,emotion\"\n",
    "}\n",
    "response3 = requests.post(request_url, data=params, headers=headers)\n",
    "baidu_r3 =response3.json()\n",
    "print(baidu_r3)\n",
    "\n",
    "baidu_df3 = pd.json_normalize(baidu_r3)\n",
    "baidu_df3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 结论\n",
    "\n",
    "### 根据以上三家API人脸识别的检测，对比了年龄、性别、情绪。 得出以下结论：\n",
    "\n",
    "+ 个人认为Azure的检测结果，相较于其他两家而言，更为精准。但是在多人混杂的图片中，一些侧脸它也还是无法准确识别出人的性别等这种人类肉眼可识别的。\n",
    "\n",
    "+ 三家API检测的单人照为同一张图片，face++检测出的年龄比Asure的要大。\n",
    "+ 其次，期间我在face++使用的一张电影《暮光之城》的9人海报照，Azure只能识别出3人，而face++能识别出5人。\n",
    "+ 而Azure检测多人混合的那张图，它对于正脸的识别却很精准。（这个我暂时还没有研究出它的问题出在哪里。。。）\n",
    "+ 百度AI开放平台——百度智能云，无论上传几张人脸的图只能检测到一张人脸信息。\n",
    "+ 而来自网络答案指出，百度AI人脸识别接口分为V2和V3两个版本，V3版本接口，不管几个人都指返回一张人脸的数据。（使用的正是v3接口官方文档）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
